<?php
namespace Api\Controller;
use Api\Model\OrderModel;
//header("content-type:text/html;charset=utf-8");
class ShopController extends ApiController{
    
    /**
    *店铺信息
    *@param int shop_id
    *
    */
    public function shopInfo(){
        params_exist(array('shop_id'));
        $shop_id=I('post.shop_id');
        $M_shop=M('shop');
        $map=array('id'=>$shop_id,'status'=>1);
        $shop_info=$M_shop->field(array('id', 'uid','title','headpic','signpic','create_time', 'gift_zhangsheng', 'gift_xiaomuzhi', 'description'))->find($shop_id);        
        empty($shop_info) && response(-2);
        $shop_info['create_time_format'] = time_format($shop_info['create_time'],'Y-m-d');
        $shop_info['headpic_path'] = get_url($shop_info['headpic']);
        $shop_info['signpic_path'] = get_url($shop_info['signpic']);
        $shop_info['im_account'] = get_username($shop_info['uid']);
        $level = calculation_shop_level($shop_info['gift_zhangsheng'], $shop_info['gift_xiaomuzhi']);
        $shop_info['level'] = $level;
        response(1, $shop_info);
    }

    /**
     * 获取商铺积分信息
     */

    public function ShopScore(){
        params_exist(array('shop_id'));
        $shop_id=I('post.shop_id');
        $M_shop=M('shop');
        $data = $M_shop->where('id='.$shop_id)->Field(array(
            'give_score','recover_score','uid','id'
        ))->find();
        if(!$data)
            response(-1,'店铺不存在！');
        if(empty($data['recover_score']))
            $data['recover_score']=0;
        if(empty($data['give_score']))
            $data['give_score']=0;
        $data['score'] = D('member')->where('uid='.$data['uid'])->getField('score');
        response(1,$data);
    }
    /**
     * 同意退款
     * @param $order_id
     */
    public function agree_refund(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        params_exist(array(
            'order_id'
        ));
        $order = D('order')->Where('id="'.I('post.order_id').'"')->find();

        if ($order){

            $products = D('order_product')->where('order_id='.$order['id'])->find();

            $uid = D('shop')->where('id='.$products['shop_id'])->getField('uid');

            $uid == API_UID || response(-1,'你无权限退款此订单');

            if($order['pay_status']==OrderModel::ORDER_REFUND){
                response(-1,'此订单已经退款成功了！');
            }

            if($order['pay_status']!=OrderModel::ORDER_APPLY_REFUND){
                response(-1,'此订单并没有申请退款！');
            }

            D('order')->where('id='.$order['id'])->setField('pay_status',OrderModel::ORDER_REFUND);
//            D('order')->where()
            D('member')->where('uid='.$order['uid'])->setInc('score_num',$order['score_num']);
            if($order['pay_style'] ==10){ //播豆支付，退还播豆
                $username=M('ucenter_member')->where('id='.$order['uid'])->getField('username');
                $r=M('users','cmf_')->where('user_login='.$username)->setInc('coin',$order['score_num']);
                $money=$order['score_num'];
                $pur_type=2;
            }else {
                //退回到余额
                $res = M('member')->where('uid=' . $order['uid'])->setInc('amount', $order['price']);
                $money=$order['price'];
                $pur_type=1;
            }
            //退款记录
            $data2=[];
            $data2['uid'] = $order['uid'];
            $data2['order_id'] = $order['id'];
            $data2['type'] = 6;
            $data2['money'] = $money;
            $data2['month'] = date('Ym',time());
            $data2['status'] = 1;
            $data2['pur_type'] = $pur_type;
            $data2['createtime'] = time();
            $b = M('user_trade')->add($data2);
            response(1);

        }else{
            response(-1,'订单不存在！');
        }
    }
    /**
     *店铺商品
     */
    public function Products(){
        params_exist(array('shop_id'));
        $shop_id=I('post.shop_id');
        $M_product=M('product');
        $M_product_image=M('product_image');
        
        $page=I('page',1);
        $pagesize=I('pagesize',10);
        $page <=0 && $page=1;
        $pagesize <=0 && $pagesize=10;
        $offset=($page-1)*$pagesize;
        $map=array('shop_id'=>$shop_id,'is_show'=>1,'status'=>1);
        
        $list=$M_product->where($map)->limit($offset,$pagesize)
        ->field('id,shop_id,uid,price,title,sales')
        ->select();
        $totle=$M_product->where($map)->count();
        foreach($list as &$item){
            $p_image=$M_product_image->where(array('product_id'=>$item['id']))->find();
            $item['image_path']=get_url($p_image['cover_id']);
        }
        $result['total']=$totle;
        $result['result']=$list;
        
        $list ? response(1,$result) : response(-2);
    }
    
    /**
     *店铺商品
     *@param string keyword
     *@param int shop_id
     */
    public function produtSearch(){
        params_exist(array('shop_id','keyword'));
        $shop_id=I('post.shop_id');
        $M_product=M('product');
        $M_product_image=M('product_image');
        $keyword=I('post.keyword');
        !preg_match('/^.{1,10}$/', $keyword) && response(-1,'','关键词在1-10字之间');
        $pagesize=I('pagesize',10);
        $page=I('post.page',1);
        $page <=0 && $page=1;
        $pagesize <=0 && $pagesize=10;
        $offset=($page-1)*$pagesize;
        $map=array('shop_id'=>$shop_id,'is_show'=>1,'status'=>1,'title|description'=>array('like','%'.$keyword.'%'));
    
        $list=$M_product->where($map)->limit($offset,$pagesize)
        ->field('id,shop_id,uid,price,title,sales')
        ->select();
        $totle=$M_product->where($map)->count();
        foreach($list as &$item){
            $p_image=$M_product_image->where(array('product_id'=>$item['id']))->find();
            $item['image_path']=get_url($p_image['cover_id']);
        }
        $result['total']=$totle;
        $result['result']=$list;
        $result['keyword']=$keyword;
    
        $list ? response(1,$result) : response(-2);
    }
}
?>
